<template>
	<div>
		<!-- 面包屑导航区域 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>订单管理</el-breadcrumb-item>
			<el-breadcrumb-item>订单列表</el-breadcrumb-item>
		</el-breadcrumb>

		<!-- 卡片视图区域 -->
		<el-card>
			<!-- 搜索输入框 -->
			<el-row>
				<el-col :span="8">
					<el-input v-model="queryInfo.query" placeholder="请输入内容" clearable @clear="getOrderList">
						<el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
					</el-input>
				</el-col>
			</el-row>

			<!-- 表格区域 -->
			<el-table :data="orderList" border stripe>
				<el-table-column label="#" type="index"></el-table-column>
				<el-table-column label="订单编号" prop="order_number"></el-table-column>
				<el-table-column label="订单价格" prop="order_price" width="80"></el-table-column>
				<el-table-column label="是否付款" width="80">
					<template slot-scope="scope">
						<el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
						<el-tag type="success" v-else>已付款</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="是否发货" prop="is_send" width="80"></el-table-column>
				<el-table-column label="下单时间" width="160">
					<template slot-scope="scope">
						{{scope.row.create_time | dateFormat}}
					</template>
				</el-table-column>
				<el-table-column label="操作" width="120">
					<template slot-scope="scope">
						<el-button size="mini" type="primary" icon="el-icon-edit" @click="showBox(scope.row.order_id)"></el-button>
						<el-button size="mini" type="success" icon="el-icon-location" @click="showProgressDialog"></el-button>
					</template>
				</el-table-column>
			</el-table>

			<!-- 分页区域 -->
			<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>

		<!-- 修改订单地址的对话框 -->
		<el-dialog title="修改订单" :visible.sync="addressVisible" width="50%" @close="addressDialogClosed">
			<el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="90px">
				<el-form-item label="省市区/县" prop="address1">
					<el-cascader v-model="addressForm.address1" :options="cityData" :props="cityProps"></el-cascader>
				</el-form-item>
				<el-form-item label="详细地址" prop="address2">
					<el-input v-model="addressForm.address2"></el-input>
				</el-form-item>
				<el-form-item label="订单编号" prop="order_number">
					<el-input v-model="addressForm.order_number"></el-input>
				</el-form-item>
				<el-form-item label="订单价格" prop="order_price">
					<el-input v-model.number="addressForm.order_price" type="number"></el-input>
				</el-form-item>
				<el-form-item label="支付状态" prop="pay_status">
					<el-input v-model="addressForm.pay_status"></el-input>
				</el-form-item>
				<el-form-item label="支付方式" prop="order_pay">
					<el-input v-model="addressForm.order_pay"></el-input>
				</el-form-item>
				<el-form-item label="是否发货" prop="is_send">
					<el-input v-model="addressForm.is_send"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="addressVisible = false">取 消</el-button>
				<el-button type="primary" @click="editOrder">确 定</el-button>
			</span>
		</el-dialog>

		<!-- 展示物流进度的对话框 -->
		<el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
			<!-- 时间线 -->
      <el-timeline>
				<el-timeline-item v-for="(activity, index) in kuaidi" :key="index" :timestamp="activity.time">
					{{activity.context}}
				</el-timeline-item>
			</el-timeline>
		</el-dialog>
	</div>
</template>

<script>
// 导入城市数据
import cityData from './citydata.js'
// 导入物流信息
import kuaidi from './kuaidi.js'

export default {
  data() {
    return {
      // 请求参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 订单列表
      orderList: [],
      // 总数居条数
      total: 0,
      // 控制修改地址对话框的展示与隐藏
      addressVisible: false,
      // 修改地址的表单数据对象
      addressForm: {
        address1: [],
        address2: '',
        is_send: '',
        order_pay: '',
        order_price: 0,
        order_number: '',
        pay_status: ''
      },
      // 修改地址表单的验证规则对象
      addressFormRules: {
        address1: [{ required: true, message: '请选择省市区县', trigger: 'blur' }],
        address2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
        is_send: [{ required: true, message: '请输入物流状态', trigger: 'blur' }],
        order_pay: [{ required: true, message: '请输入支付方式', trigger: 'blur' }],
        order_price: [{ required: true, message: '请输入订单价格', trigger: 'blur' }],
        order_number: [{ required: true, message: '请输入订单编号', trigger: 'blur' }],
        pay_status: [{ required: true, message: '请输入支付状态', trigger: 'blur' }]
      },
      // 外界导入的省市区县数据
      cityData,
      cityProps: {
        expandTrigger: 'hover',
        value: 'value',
        label: 'label',
        children: 'children'
      }, // 即将修改订单状态的id
      orderId: '',
      // 控制物流进度对话框的展示与隐藏
      progressVisible: false,
      // 物流信息
      kuaidi
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表数据
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取订单列表失败！')
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听 pagenum 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 监听修改地址对话框的关闭事件
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 展示修改地址对话框
    async showBox(orderId) {
      // 查询订单详情
      const { data: res } = await this.$http.get('orders/' + orderId)
      if (res.meta.status !== 200) return this.$message.error('获取订单数据失败！')
      this.orderId = orderId
      this.addressForm.is_send = res.data.is_send
      this.addressForm.order_pay = res.data.order_pay
      this.addressForm.order_price = res.data.order_price
      this.addressForm.order_number = res.data.order_number
      this.addressForm.pay_status = res.data.pay_status

      this.addressVisible = true
    },
    // 修改订单并提交
    editOrder() {
      this.$refs.addressFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('orders/' + this.orderId, {
          is_send: this.addressForm.is_send,
          order_pay: this.addressForm.order_pay,
          order_price: this.addressForm.order_price,
          order_number: this.addressForm.order_number,
          pay_status: this.addressForm.pay_status
        })
        // console.log(res)
        if (res.meta.status !== 201) return this.$message.success('更新失败！')
        this.addressVisible = false
        this.getOrderList()
        this.$message.success('更新成功！')
      })
    },
    // 展示物流进度对话框
    showProgressDialog() {
      this.progressVisible = true
    }
    // 获取物流信息，接口废了，自己 mock 了一份数据
    // async getProgressInfo() {
    //   const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
	width: 100%;
}
</style>
